﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.Model;

namespace DuJianliang.ApplicationPlatform.UserPermission
{
    public class UserPermissionBll
    {
        public DataTable GetDeptDataTable()
        {
            try
            {
                UserPermissionDal userPremissionDal = new UserPermissionDal();
                return userPremissionDal.GetDeptDataTable();
            }
            catch (Exception ex)
            {
                throw ex;
            } 
        }

        public DeptOB GetDeptByID(string deptID)
        {
            try
            {
                UserPermissionDal userPremissionDal = new UserPermissionDal();
                return userPremissionDal.GetDeptByID(deptID);
            }
            catch (Exception ex)
            {
                throw ex;
            } 
        }

        public DeptOB GetDeptByUserID(string userID)
        {
            try
            {
                UserPermissionDal userPremissionDal = new UserPermissionDal();
                DataTable deptDataTable = userPremissionDal.GetDeptDataTable();
                DeptOB deptOB = userPremissionDal.GetDeptByUserID(userID);
                InitDeptFullName(deptDataTable, deptOB.ParentDeptID, deptOB);
                deptOB.DeptFullName = deptOB.DeptFullName + deptOB.DeptName;
                return deptOB;
            }
            catch (Exception ex)
            {
                throw ex;
            }            
        }

        private void InitDeptFullName(DataTable deptDataTable, string parentDeptID, DeptOB deptOB)
        {
            if (!string.IsNullOrWhiteSpace(parentDeptID))
            {
                DataRow[] dataRows = deptDataTable.Select(string.Format("DeptID={0}", parentDeptID));
                if (dataRows.Length > 0)
                {
                    deptOB.DeptFullName = dataRows[0]["DeptName"].ToString() + "-" + deptOB.DeptFullName;
                    InitDeptFullName(deptDataTable, dataRows[0]["ParentDeptID"].ToString(), deptOB);
                }
            }            
        }

        public string GetDeptFullName(string deptID)
        {
            try
            {
                UserPermissionDal userPremissionDal = new UserPermissionDal();
                DataTable deptDataTable = userPremissionDal.GetDeptDataTable();
                DeptOB deptOB = userPremissionDal.GetDeptByID(deptID);
                InitDeptFullName(deptDataTable, deptOB.ParentDeptID, deptOB);
                return deptOB.DeptFullName + deptOB.DeptName;
            }
            catch (Exception ex)
            {
                throw ex;
            }  
        }

        // 获取所有子部门
        public List<DeptOB> GetChildDeptID(string deptID)
        {
            UserPermissionDal dal = new UserPermissionDal();
            List<DeptOB> deptOBList = new List<DeptOB>();
            DataTable deptDataTable = dal.GetDeptDataTable();
            SetChildDeptID(dal, deptDataTable, deptID, deptOBList);
            return deptOBList;
        }

        private void SetChildDeptID(UserPermissionDal dal, DataTable deptDataTable, string deptID, List<DeptOB> deptOBList)
        {
            if (!string.IsNullOrWhiteSpace(deptID))
            {
                DataRow[] dataRows = deptDataTable.Select(string.Format("ParentDeptID={0}", deptID));
                foreach (DataRow dataRow in dataRows)
                {
                    DeptOB deptOB = dal.GetDeptByID(dataRow["DeptID"].ToString());
                    deptOBList.Add(deptOB);
                    SetChildDeptID(dal, deptDataTable, deptOB.DeptID, deptOBList);
                }                
            }
        }

        public string GetChildDeptIDString(string deptID)
        {
            List<DeptOB> deptOBs = this.GetChildDeptID(deptID);
            string childDeptID = string.Empty;

            foreach (DeptOB deptOB in deptOBs)
            {
                childDeptID += deptOB.DeptID + ",";
            }

            return childDeptID.TrimEnd(',');
        }

        public bool HasPermission(string userID, int permissionID)
        {
            UserInfo userInfo = CurrentInfo.Instance.CurrentUser;
            if (userInfo.ID.Equals(userID) && userInfo.IsAdmin)
            {
                return true;
            }

            try
            {
                try
                {
                    UserPermissionDal userPremissionDal = new UserPermissionDal();
                    return userPremissionDal.HasPermission(userID, permissionID);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool IsAdmin(string userID)
        {
            try
            {
                UserPermissionDal userPremissionDal = new UserPermissionDal();
                return userPremissionDal.IsAdmin(userID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool hasOtherAdmin(string userID)
        {
            try
            {
                UserPermissionDal userPremissionDal = new UserPermissionDal();
                return userPremissionDal.hasOtherAdmin(userID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable GetPermissionDataTable()
        {
            try
            {
                UserPermissionDal userPremissionDal = new UserPermissionDal();
                return userPremissionDal.GetPermissionDataTable();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable GetUserPermissionDataTable(string userID)
        {
            try
            {
                UserPermissionDal userPremissionDal = new UserPermissionDal();
                return userPremissionDal.GetUserPermissionDataTable(userID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}